<?php
class Controller_Admin extends Controller_DefaultTemplate {

    function action_index() {
        $username = "";
        $password = "";
        $error = "";

        $username =session::instance()->get('username');
        if(isset($username)) {
            $this->request->redirect("../admin/main/");
        }

        if(isset($_POST['username'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];

            $sql = "SELECT *  FROM admin where username='".$username."'";
            $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

            if($query==null) {
                $error = "Invalid Username of password";
            }else {
                if($query[0]['password']==$password) {
                    session::instance()->set('username',$username);
                    session::instance()->set('role','admin');
                    $this->request->redirect("../admin/main/");
                }else {
                    $error = "Invalid Username of password";
                }
            }
        }

        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("admin/login")
                ->set('username',$username)
                ->set('error',$error);
    }

    function action_main() {
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/main");
            }
        }
    }

    function action_patient_edit() {
        $p_id = $_GET['p_id'];
        $message = "";
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $patientModel = new Model_Patient();
                if(isset($_GET['method'])) {
                    $method = $_GET['method'];
                    if($method=='edit') {
                        $patientModel->updateProfile($_GET);
                        $message = "แก้ไขข้อมูลเรียบร้อย";
                    }
                }

                $patient = $patientModel->getPatientProfileFromPID($p_id);

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/patient_edit")
                        ->set('message',$message)
                        ->set('p_id',$p_id)
                        ->set('patient',$patient[0]);
            }
        }
    }

    function action_patient_del() {
        $p_id = $_GET['p_id'];

        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $patientModel = new Model_Patient();
                $patientModel->delPatient($p_id);
                $this->request->redirect("../admin/patient");
            }
        }
    }

    function action_patient() {
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $patient = new Model_Patient();

                $patient_list = $patient->getAllPatient();

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/patient")
                        ->set('patient_list',$patient_list);
            }
        }
    }

    function action_doctor() {
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $doctors = new Model_Doctor();

                $doctor_list = $doctors->getAllDoctor();

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/doctor")
                        ->set('doctor_list',$doctor_list);
            }
        }
    }

    function action_doctor_add(){
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                if(isset($_POST['method'])){
                    $doctor = new Model_Doctor();
                    $doctor->addProfile($_POST);                   
                    $this->request->redirect("../admin/doctor");
                }
                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/doctor_add");
            }
        }
    }

    function action_doctor_edit() {
        if(isset($_GET['consult_doctor_id']))
            $consult_doctor_id = $_GET['consult_doctor_id'];

        if(isset($_POST['consult_doctor_id']))
            $consult_doctor_id = $_POST['consult_doctor_id'];

        $message = "";
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $doctorModel = new Model_Doctor();
                if(isset($_POST['method'])) {
                    $method = $_POST['method'];
                    if($method=='edit') {
                        $doctorModel->updateProfile($_POST);
                        $message = "แก้ไขข้อมูลเรียบร้อย";
                    }
                }

                $doctor = $doctorModel->getDoctorProfileFromConsult_doctor_id($consult_doctor_id);

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/doctor_edit")
                        ->set('message',$message)
                        ->set('consult_doctor_id',$consult_doctor_id)
                        ->set('doctor',$doctor[0]);
            }
        }
    }

    function action_doctor_del() {
        $consult_doctor_id = $_GET['consult_doctor_id'];

        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $doctorModel = new Model_Doctor();
                $doctorModel->delDoctor($consult_doctor_id);
                $this->request->redirect("../admin/doctor");
            }
        }
    }

    function action_ipcamera(){
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $place = new Model_Place();

                $place_list = $place->getAllPlace();

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/place")
                        ->set('list',$place_list);
            }
        }
    }

    function action_ipcamera_edit() {
        if(isset($_GET['pp_id']))
            $pp_id = $_GET['pp_id'];

        if(isset($_POST['pp_id']))
            $pp_id = $_POST['pp_id'];

        $message = "";
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $placeModel = new Model_Place();
                if(isset($_POST['method'])) {
                    $method = $_POST['method'];
                    if($method=='edit') {
                        $placeModel->updateProfile($_POST);
                        $message = "แก้ไขข้อมูลเรียบร้อย";
                    }
                }

                $place = $placeModel->getDataFromPP_id($pp_id);

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/place_edit")
                        ->set('message',$message)
                        ->set('pp_id',$pp_id)
                        ->set('place',$place[0]);
            }
        }
    }

    function action_ipcamera_add(){
  
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $placeModel = new Model_Place();
                if(isset($_POST['method'])) {
                    $method = $_POST['method'];
                    if($method=='add') {
                        $placeModel->add($_POST);                        
                    }
                }

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("admin/place_add");
            }
        }
    }

    function action_ipcamera_del() {
        $pp_id = $_GET['pp_id'];

        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../admin/");
        }else {
            if(session::instance()->get('role')!="admin") {
                echo "You don't have permission to access this page";
            }else {
                $model = new Model_Place();
                $model->del($pp_id);
                $this->request->redirect("../admin/ipcamera");
            }
        }
    }

    function action_logout() {
        session::instance()->delete('username');
        $this->request->redirect("../");
    }
}
?>
